package xin.yangshuai.swagger2.support;

import java.util.ArrayList;
import java.util.List;

/**
 * InitData
 *
 * @author shuai
 * @date 2020/5/24
 */
public class InitData {

    public static List<User> userList = new ArrayList<>();

    static {

        User u1 = new User();
        User u2 = new User();
        User u3 = new User();

        u1.setId(1).setName("张三").setAge(18);
        u2.setId(2).setName("李四").setAge(20);
        u3.setId(3).setName("王五").setAge(24);

        userList.add(u1);
        userList.add(u2);
        userList.add(u3);
    }

    public static List<User> list() {
        return userList;
    }

    public static User get(Integer id) {
        for (User user : userList) {
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }

    public static int insert(User user) {
        if (user == null) {
            return 0;
        }
        userList.add(user);
        return 1;
    }

    public static int update(User user) {
        if (user == null) {
            return 0;
        }
        for (User u : userList) {
            if (u.getId().equals(user.getId())) {
                u = user;
                break;
            }
        }
        return 1;
    }

    public static int delete(Integer id) {
        if (id == null) {
            return 0;
        }
        for (User user : userList) {
            if (user.getId().equals(id)) {
                userList.remove(user);
                break;
            }
        }
        return 1;
    }


}
